/*
 * @(#)WorkerThread.java    �汾�� 2010-10-30
 * 
 * Version information
 * 
 * Copyright notice
 */

package cn.edu.nju.software.localserver;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;

import cn.edu.nju.software.beans.CourseBean;
import cn.edu.nju.software.beans.StudentBean;

/**
 * Class description goes here.
 * 
 * @version �汾�� 2010-10-30
 * @author lijinsheng
 */
public class WorkerThread extends Thread {
	/* One Thread handle one client */
	private Socket client = null;
	private DataOutputStream outputStream = null;
	public WorkerThread(Socket client) {
		this.client = client;
		
		try {
			outputStream = new DataOutputStream(new BufferedOutputStream(
					client.getOutputStream()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Socket getClient() {
		return client;
	}

	public void setClient(Socket client) {
		this.client = client;
	}

	public void run() {
		DataInputStream inputStream = null;
		try {
			inputStream = new DataInputStream(new BufferedInputStream(
					this.client.getInputStream()));
		} catch (Exception e) {
			return;
		}

		while (true) {
			try {
				if (inputStream.available() != 0) {
					if(this.parse(inputStream)) {
						break;
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				break;
			}
		}
	}

	private boolean parse(DataInputStream inputStream){
		int messageType = -1;
		try {
			messageType = inputStream.readInt();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				inputStream.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return true;
		}

		switch (messageType) {
		case 1:
			LogType logType = Handler.handleLog(inputStream);
			switch (logType) {
			case Administrator:
				try {
					outputStream.writeInt(1);
					outputStream.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return true;
				}
				break;
			case Student:
				try {
					outputStream.writeInt(2);
					outputStream.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return true;
				}
				break;
			case LogFail:
				try {
					outputStream.writeInt(3);
					outputStream.flush();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return true;
				}
				break;
			}
			break;
		case 2:
			try {
				outputStream.writeBoolean(Handler
						.handleChangePassword(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 3:
			for (String oneInformation : Handler
					.handlePerInformationView(inputStream)) {
				try {
					outputStream.writeUTF(oneInformation);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return true;
				}
			}

			try {
				outputStream.flush();
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
				return true;
			}
			break;
		case 4:
			ArrayList<ArrayList<CourseBean>> allCourses = Handler
					.handleCourseView(inputStream);

			for (CourseBean course : allCourses.get(0)) {
				try {
					outputStream.writeUTF(course.getId());
					outputStream.writeUTF(course.getName());
					outputStream.writeUTF(course.getTeacher());
					outputStream.writeInt(course.getGreads());
					outputStream.writeInt(course.getNumber());
					outputStream.writeUTF(course.getPlace());
					outputStream.writeUTF(course.getInstitute());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return true;
				}
			}
			try {
				outputStream.writeUTF("unchosen_end");
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			for (CourseBean course : allCourses.get(1)) {
				try {
					outputStream.writeUTF(course.getId());
					outputStream.writeUTF(course.getName());
					outputStream.writeUTF(course.getTeacher());
					outputStream.writeInt(course.getGreads());
					outputStream.writeInt(course.getNumber());
					outputStream.writeUTF(course.getPlace());
					outputStream.writeUTF(course.getInstitute());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return true;
				}
			}
			try {
				outputStream.writeUTF("course_end");
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 5:
			try {
				outputStream.writeBoolean(Handler
						.handleChooseCourse(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 6:

			for (CourseBean course : Handler.handleAdministratorCourseView()) {
				try {
					outputStream.writeUTF(course.getId());
					outputStream.writeUTF(course.getName());
					outputStream.writeUTF(course.getTeacher());
					outputStream.writeInt(course.getGreads());
					outputStream.writeInt(course.getNumber());
					outputStream.writeUTF(course.getPlace());
					outputStream.writeUTF(course.getInstitute());
					outputStream.writeBoolean(course.getShared());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return true;
				}
			}

			try {
				outputStream.writeUTF("course_end");
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 7:
			try {
				outputStream.writeBoolean(Handler.handleAddCourse(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 8:
			try {
				outputStream.writeBoolean(Handler
						.handleDeleteCourse(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 9:

			for (StudentBean student : Handler.handleAdministratorStudentView()) {
				try {
					outputStream.writeUTF(student.getId());
					outputStream.writeUTF(student.getName());
					outputStream.writeUTF(student.getGender());
					outputStream.writeUTF(student.getInstitute());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return true;
				}
			}

			try {
				outputStream.writeUTF("student_end");
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 10:
			try {
				outputStream
						.writeBoolean(Handler.handleAddStudent(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 11:
			try {
				outputStream.writeBoolean(Handler
						.handleDeleteStudent(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 12:
			try {
				outputStream.writeBoolean(Handler.handleShareCourse());
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 13:
			File xmlFile = Handler.handleFromIntegrationCourseShare();
			FileHandler.writeFile(xmlFile, outputStream);
			break;
		case 14:
			try {
				outputStream.writeBoolean(Handler
						.handleFromIntegrationCourseSelect(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 15:
			try {
				outputStream.writeBoolean(Handler
						.handleFromIntegrationCourseUnScibe(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 16:
			try {
				outputStream.writeBoolean(Handler
						.handleFromIntegrationDeleteCourse(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		case 17:
			try {
				outputStream.writeBoolean(Handler
						.handleFromIntegrationDeleteStudent(inputStream));
				outputStream.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return true;
			}
			break;
		default:
		}
		return false;
	}
}
